package cn.jdy.plugins.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.common.constant.GlobalConstant;
import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.framework.util.NginxPurgeCacheUtil;
import cn.jdy.platform.service.ConfService;
import cn.jdy.plugins.admin.export.RedPacketExcelView;
import cn.jdy.plugins.admin.export.SecretkeylExcelView;
import cn.jdy.plugins.entity.EvaluationProject;
import cn.jdy.plugins.entity.Line;
import cn.jdy.plugins.entity.RedPacket;
import cn.jdy.plugins.entity.SecretKeyl;
import cn.jdy.plugins.service.LiveService;
import cn.jdy.plugins.service.SecretKeylService;
import cn.jdy.plugins.wap.util.ChatUtil;
 

/**
 * @title 商家管理后台操作
 * @author jga
 * @since 1-12-2015
 */
@Controller
@RequestMapping("/secretkeyl")
public class SecretkeylCtrl extends BaseCtrl{
	@Autowired
	private SecretKeylService SecretkeylService;
	@Autowired
	private ConfService confService;
	//默认列表路径
	private static String viewName = "redirect:/secretkeyl/list?s=";
	@Autowired
	private LiveService liveService;
	/**
	 *  初始化
	 * @param entity
	 * @param fromDate
	 * @param toDate
	 * @param s
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(SecretKeyl entity, String fromDate, String toDate, @RequestParam(required = false) Character s) {
		ModelAndView mv = new ModelAndView("/secretKeyl/secretKeylList.jsp");
		if(s != null)
			switch(s) {
				case '1': mv.addObject("msg", "保存成功！");break;
				case '2': mv.addObject("msg", "更新成功！");break;
				case '3': mv.addObject("msg", "删除成功！");break;
				case '4': mv.addObject("msg", "操作失败！");break;
				default: break;
			}
    
		mv.addObject("liveId", entity.getLiveId());
		mv.addObject("entity", entity);
		return mv;
	}
	
	/**
	 * 异步查询
	 * @param sp
	 * @param fromDate
	 * @param toDate
	 * @return
	 */
	@RequestMapping("/queryByPage")
	@ResponseBody
	public Map<String, Object> queryByPage(SecretKeyl sp){
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			jsonMap.put("dataList", SecretkeylService.query(sp));
			jsonMap.put("iTotalDisplayRecords", SecretkeylService.count(sp));
			jsonMap.put("iTotalRecords", SecretkeylService.count(sp));
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "查询异常", e);
		}
		return jsonMap;
	}
	
	/**
	 * 新增
	 * @return
	 */
/*	@RequestMapping("/edit")
	public ModelAndView edit() {
		return edit(null);
	}*/
	
	/**
	 * 编辑
	 * @param id
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit(SecretKeyl p){
		ModelAndView mv = new ModelAndView("/secretKeyl/secretKeylDetail.jsp");
		if(p.getLiveId()!=null){
			mv.addObject("datalists", liveService.find("id",p.getLiveId()));
			}else{
			mv.addObject("datalists", liveService.findAll());
			}
		mv.addObject("data",  p.getId()==null|| p.getId()==0 ?  p.getId() : this.SecretkeylService.find(p.getId()));
		return mv;
	}
	
	/**
	 * 保存信息
	 * @param sp
	 * @param pid
	 * @return
	 */
	@RequestMapping("/save")
	 @ResponseBody
	 public ModelAndView save(SecretKeyl sp) {
		ModelAndView mv = new ModelAndView();
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
		if(sp.getId()==null) {//
				sp.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				this.SecretkeylService.save(sp);
				mv.setViewName("redirect:/secretkeyl/list?liveId="+sp.getLiveId());
			} else {//更新
			sp.setUpdateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				this.SecretkeylService.update(sp);
				mv.setViewName("redirect:/secretkeyl/list?liveId="+sp.getLiveId());
			}
    jsonMap.put("statusCode", 200);
		} catch(Exception e) {
      mv.addObject("statusCode", "500");
			mv.setViewName(this.viewName.concat("4"));
			BaseLog.e(getClass(), "信息保存异常！", e);
		}

	   return mv;
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/del/{id}")
	@ResponseBody
	public Map<String, Object> del(@PathVariable Integer id) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			this.SecretkeylService.delete(id);
			jsonMap.put("msg", "成功");
	      // 清理内容页缓存
		} catch(Exception e) {
			jsonMap.put("msg", "失败");
	      BaseLog.e(getClass(), "删除异常！", e);
		}
		return jsonMap;
	}


	/**
	 * 保存信息
	 * @param sp
	 * @param pid
	 * @return
	 */
	@RequestMapping("/addMiyao")
	 @ResponseBody
		public Map<String, Object> addMiyao() {
  	String miyao = ChatUtil.generateString(13);
	Map<String, Object> jsonMap = new HashMap<String, Object>();
	jsonMap.put("miyao",miyao );
	   return jsonMap;
  	
	}
	
	/**
	 * 批量生成秘钥
	 * @param sp
	 * @param pid
	 * @return
	 */
	@RequestMapping("/saveSecretKeyls")
	 @ResponseBody
	 public  Map<String, Object>  saveSecretKeyls(int sz,Long liveId) {
		ModelAndView mv = new ModelAndView();
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
		    for (int i = 0; i < sz; i++) {
		    SecretKeyl sp=new SecretKeyl();
			sp.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
			sp.setLiveId(liveId);
			sp.setPasswork(ChatUtil.generateString(13));
			this.SecretkeylService.save(sp);
		    }
				
			 
    jsonMap.put("statusCode", "200");
		} catch(Exception e) {
      mv.addObject("statusCode", "500");
			mv.setViewName(this.viewName.concat("4"));
			BaseLog.e(getClass(), "信息保存异常！", e);
		}

	   return jsonMap;
	}
	
	/**
	 *  导出秘钥记录
	 * @param evaluationRecord
	 * @param fromDate
	 * @param toDate
	 * @param model
	 * @return
	 */
	@RequestMapping("/secretkeylQueryByPage")
	public ModelAndView secretkeylQueryByPage(SecretKeyl rpt,String fromDate, String toDate,ModelMap model) {
	    List<SecretKeyl> list = new ArrayList<SecretKeyl>();
	    DetachedCriteria  dc= DetachedCriteria.forClass(SecretKeyl.class);
			  dc.add(Restrictions.eq("liveId",rpt.getLiveId()));
			  dc.addOrder(Order.desc("createTime"));
			  list =SecretkeylService.find(dc);
	
	    model.put("dataSet", list);
	    model.put("searchDate", fromDate+"-"+toDate);
	    SecretkeylExcelView secretkeylExcelView = new SecretkeylExcelView();
	    return new ModelAndView(secretkeylExcelView, model);
	}
  
}
